package com.zjw.project.oauth;

import com.zjw.project.weixin.main.MenuManager;
import com.zjw.project.weixin.util.WeixinUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @Classname WeixinOauth
 * @Author guoweixin
 * @Description TODO
 * @Date 2021/1/26 14:34
 * @Created by Administrator
 */
@RequestMapping("weixin")
@Controller
@Slf4j
public class WeixinOauth {
    // weixin/oauth
    @RequestMapping("oauth")
    public void oauth(HttpServletResponse response) throws IOException {
        //用户是否同意授权
        String path="http://zjw666.nat300.top/weixin/invoke";
        try {
            path=  URLEncoder.encode(path,"UTF-8");
            //https%3A%2F%2Fpassport.baidu.com%2Fphoenix%2Faccount%2Fafterauth%
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        String url="https://open.weixin.qq.com/connect/oauth2/authorize" +
                "?appid=" + MenuManager.appId+
                "&redirect_uri=" +path+
                "&response_type=code" +
                "&scope=snsapi_userinfo" +
                "&state=java2005" +
                "#wechat_redirect";
        response.sendRedirect(url);
    }
    // 如果用户同意授权，页面将跳转至 redirect_uri/?code=CODE&state=STATE。
    //  http://java1.natapp1.cc/oauth/invoke/?code=CODE&state=STATE
    @RequestMapping("invoke")

    public String invoke(HttpServletRequest request){
        //2 用户同意授权  获取code
        String code= request.getParameter("code");
        log.info("得到授权码code："+code);
        String state=request.getParameter("state");
        //3 根据code 向腾讯服务器发送接口请求（认证服务器）
        // 获取code后，请求以下链接获取access_token：
        // https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
        String url="https://api.weixin.qq.com/sns/oauth2/access_token" +
                "?appid=" +MenuManager.appId+
                "&secret=" +MenuManager.appSecret+
                "&code=" +code+
                "&grant_type=authorization_code";
        JSONObject jsonObject= WeixinUtil.httpRequest(url,"GET",null);
        String access_token=jsonObject.getString("access_token");
        String openid=jsonObject.getString("openid");
        log.info("认证服务器返回的access_token："+jsonObject);

        //4 根据access_token和openid 向腾讯服务器发送接口请求（资源服务器）
        //http：GET（请使用https协议） https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
        String url1="https://api.weixin.qq.com/sns/userinfo?" +
                "access_token=" +access_token+
                "&openid=" +openid+
                "&lang=zh_CN";

        JSONObject jsonObject1= WeixinUtil.httpRequest(url1,"GET",null);
        log.info("用户详细信息："+jsonObject1.toString());
        request.setAttribute("jsonObject",jsonObject1);
        // /templates/oauth.html
        return "oauth";
    }
}
